#define FOR_TEST

#include <iostream>
#include <httplib.h>
#include <json.hpp>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <vector>
#include "log.hpp"
#include <base64.hpp>

using namespace std;
using namespace xzt::log;

int main()
{
    httplib::Server svr;

    httplib::Server::Handler handler = [](const httplib::Request& req, httplib::Response& res) {
        auto file = req.get_file_value("content");
        std::string buf(file.content.begin(), file.content.end());

        // 将字节数组编码为Base64
        std::string encoded_image = base64_encode(buf);

        // 创建JSON对象并添加Base64编码的图片数据
        nlohmann::json j;
        j["content"] = encoded_image;

        // 返回JSON响应
        res.set_content(j.dump(), "application/json");

        // 打印日志
        LOG_INFO("Received content with size: " + std::to_string(file.content.size()));
        LOG_INFO("file.filename: " + file.filename);
        LOG_INFO("file.content_type: " + file.content_type);
        LOG_INFO("file.name: " + file.name);
    };

    svr.Post("/upload", handler);

    svr.listen("0.0.0.0", 8787);
    return 0;
}
